//
//  HotCommunity.m
//  FreeTravel
//
//  Created by 徐康 on 16/9/9.
//  Copyright © 2016年 KX. All rights reserved.

#import "CellOfHotCommunity.h"
#import "FreeTravelMacro.h"
#import "HotCommunity.h"
#import "FreeTravelNetwork.h"
#import "ModelOfHotCommunity.h"
#import "TextAdaptation.h"
#import "MJRefresh.h"
#import "ModelOfHotCommunityData.h"
#import "WebViewOfCommendHead.h"
#import "ModelOfHotCommunityData.h"
#import "CommunityViewController.h"
@interface HotCommunity ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ModelOfHotCommunity *model;

@end
@implementation HotCommunity
//定义一个全局变量用来持续加载
{
    NSInteger n;
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        n = 1;
        [self handleData];
       
        [self createTableView];
        [self dropDownRefresh];
        [self drowDownLoad];
    }
    return self;
}
- (void)createTableView{
    self.tableView = [[UITableView alloc]initWithFrame:self.contentView.bounds style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[CellOfHotCommunity class] forCellReuseIdentifier:@"CellOfHotCommunity"];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.model.data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CellOfHotCommunity *cell = [tableView dequeueReusableCellWithIdentifier:@"CellOfHotCommunity"];
    cell.model = self.model.data[indexPath.row];
    return cell;
}

- (void)handleData{
    
    [FreeTravelNetwork GET:@"http://open.qyer.com/qyer/community/hotbbs/index?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=10&page=1&track_app_channel=App%2520Store&track_app_version=7.0.1&track_device_info=iPhone%25205s&track_deviceid=E9CA001A-5603-4CA8-B2EA-2BE34F691AC6&track_os=ios%25209.3.4&v=1" andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        self.model =[ModelOfHotCommunity modelWithDic:result];
      
        [self.tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
}
#pragma mark - 下拉刷新
- (void)dropDownRefresh{
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(handleNewData)];
}
-(void)handleNewData{
    self.model = [ModelOfHotCommunity modelWithDic:[NSDictionary dictionary]];
    
    
    [self handleData];
    
    [self.tableView.mj_header endRefreshing];
    
}
#pragma mark - 上拉加载
- (void)drowDownLoad{
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(handleMoreData)];
}
- (void)handleMoreData{
    
    n = n + 1;
    NSString *str = [NSString stringWithFormat:@"http://open.qyer.com/qyer/community/hotbbs/index?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=10&page=%ld&track_app_channel=App%%2520Store&track_app_version=7.0.1&track_device_info=iPhone%%25205s&track_deviceid=E9CA001A-5603-4CA8-B2EA-2BE34F691AC6&track_os=ios%%25209.3.4&v=1",n ];
    [FreeTravelNetwork GET:str andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
        NSMutableArray *array =[result objectForKey:@"data"] ;
        for (NSDictionary *dic in array) {
            ModelOfHotCommunityData *model = [ModelOfHotCommunityData modelWithDic:dic];
            [self.model.data addObject:model];
        }
        
        [self.tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
    
    [self.tableView.mj_footer endRefreshing];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    WebViewOfCommendHead *vc = [[WebViewOfCommendHead alloc]init];
    ModelOfHotCommunityData *model = self.model.data[indexPath.row];
    vc.url = model.appview_url;
    vc.tid = model.ID;
    
    CommunityViewController *community = (CommunityViewController *)self.nextResponder.nextResponder.nextResponder;
    [community.navigationController pushViewController:vc animated:YES];
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ModelOfHotCommunityData *model = self.model.data[indexPath.row];
    NSLog(@"%ld",model.bigpic_arr.count);
    CGSize size = [TextAdaptation labelRectWithSize:CGSizeMake(SCREEN_WIDTH-60, CGFLOAT_MAX) LabelText:model.title Font:[UIFont systemFontOfSize:15]];
    if (model.bigpic_arr.count>0) {
        return size.height+90+(SCREEN_WIDTH-80)/3;
    }else{
        return size.height+90;
    }
}
- (void)layoutSubviews{
    [super layoutSubviews];
    self.tableView.frame = self.contentView.bounds;
}
@end
